﻿
let AddmyFlavorTypes = document.querySelector(".AddmyFlavorTypes");
let FlavorType = {
    name: ''
};

AddmyFlavorTypes.onclick = function () {
    layer.open({
        title: '添加类型',
        area: ['600px', '200px'],
        formType: 0,
        content: `<div class='Input-Control'> <div class="input-text"><p>口味类型名称</p></div> <input type="text" id="FlavorType" class="text-input" required name="name" autocomplete="off" value="" placeholder="请输入口味类型名称" /></div>`,
        yes: function (index, text) {

            var values = $('#FlavorType').val();
            if (values == "") {
                Peng.Show("口味类型名称不能为空", { type: "Danger" });
                
            } else {
                FlavorType.name = values;
                $.post("/Goods/AddFlavorType", FlavorType).then((res) => {
                    if (res.code == 0) {
                        Peng.Show(res.message, { type: 'Success' });
                        layer.close(index);
                    } else {
                        Peng.Show(res.message, { type: 'Danger' });
                        

                    }
                })
            }

        }
    });
}

var loadTable
function BindFlavor(obj) {
    layui.use("table", function () {
        var table = layui.table;

        table.render({
            elem: "#test"
            , data: obj
            , align: 'center'
            , cellMinWidth: 80
            , cols: [[ //表头

                { field: 'name', title: '口味类型名称', align: 'center' }
                , { field: 'FlaoveCount', title: '口味种类', align: 'center' }
                , { title: '操作', align: 'center', toolbar: "#barDemo" }

            ]],
        });
        Peng.closeload(loadTable);

        table.on('tool(table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            switch (layEvent) {
                case "detail":
                    sessionStorage.setItem("id", data.id)
                    sessionStorage.setItem("name", data.name)
                    location.href = "/Goods/FlavorHtml"
                    break;
                case "edits":
                   
                    layer.open({
                        title: '编辑口味类型',
                        area: ['600px', '200px'],
                        content: `<div class='Input-Control'> <div class="input-text"><p>口味类型名称</p></div> <input type="text" id="FlavorssType" class="text-input" required name="name" autocomplete="off" value=${data.name} placeholder="请输入口味类型名称" /></div>`,
                        yes: function (index) {
                            $.post("/Goods/UpdateFlavorType", {
                                id: data.id,
                                name:$("#FlavorssType").val()
                            }).then(res => {
                                if (res.code == 0) {
                                    Peng.Show(res.message, { type: "Success" });
                                    layer.close(index);
                                    GetFlavorType();
                                } else {
                                    Peng.Show(res.message, { type: "Danger" });
                                   
                                }
                            })
                        }
                    })
                    break;
            }
        });
    });

}
window.onload = function () {
    GetFlavorType();
}
function GetFlavorType(key) {
    loadTable = Peng.load({ box: 'Cols', style: 'circle', color: 'ZhutiSe', content: '加载口味类型数据中.....' });
    $.get("/Goods/GetFlavorType", { keys: key }).then(res => {
        BindFlavor(res.data);
    });
}
var query = function () {
    GetFlavorType($("#MyFlavors").val())
}

